#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
# author: 小菠萝测试笔记
# blog:  https://www.cnblogs.com/poloyy/
# time: 2021/10/15 9:14 上午
# file: user.py
"""
import uuid
from datetime import datetime
from typing import List, Optional

from pydantic import BaseModel, EmailStr, Field, IPvAnyAddress

__all__ = ["UserCreate", "UserUpdate", "User", "UserList"]


class UserBase(BaseModel):
    user_id: Optional[str] = str(uuid.uuid1()).replace("-", "")
    name: str = Field(
        ..., min_length=2, max_length=8, regex="^[a-zA-Z0-9\u4e00-\u9fa5_\-\_\.]{2,8}$"
    )
    username: str = Field(
        ...,
        min_length=8,
        max_length=16,
        regex="^[a-zA-Z][a-zA-Z0-9\u4e00-\u9fa5_]{7,15}$",
    )
    email: EmailStr
    phone: str = Field(
        ...,
        min_length=11,
        max_length=11,
        regex="^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$",
    )
    avatar: Optional[str] = None
    ip: Optional[IPvAnyAddress] = None


class UserCreate(UserBase):
    password: str = Field(
        ...,
        min_length=8,
        max_length=16,
        regex="((?=.*[a-z])(?=.*\d)|(?=[a-z])(?=.*[<>;':\",.$#@!~%^&*])|(?=.*\d)(?=.*[<>;':\",.$#@!~%^&*]))[a-z\d<>;':\",.$#@!~%^&*]{8,16}",
    )
    is_super: Optional[bool] = False


class UserUpdate(UserBase):
    id: int
    password: str = Field(
        ...,
        min_length=8,
        max_length=16,
        regex="((?=.*[a-z])(?=.*\d)|(?=[a-z])(?=.*[<>;':\",.$#@!~%^&*])|(?=.*\d)(?=.*[<>;':\",.$#@!~%^&*]))[a-z\d<>;':\",.$#@!~%^&*]{8,16}",
    )
    last_login_time: Optional[datetime]
    is_super: Optional[bool] = False


class UserInDbBase(UserBase):
    id: int
    last_login_time: Optional[datetime]
    create_time: datetime
    update_time: datetime
    state: int
    is_super: bool
    is_delete: bool

    class Config:
        orm_mode = True


class User(UserInDbBase):
    pass


class UserInDb(UserInDbBase):
    password: str


class UserList(BaseModel):
    users: List[User]
    total: int
    page: int
    size: int
